﻿package mortal.game.view.copy.groupCopy.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.resource.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.copy.defenseCopy.panel.*;
    import mortal.game.view.copy.groupCopy.data.*;
    import mortal.game.view.copy.groupCopy.item.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;

    public class GroupCopyDefensePanel extends GroupCopyBasePanel
    {
        private var _enterBtn:GLabelButton;
        protected var _againSp:GroupCopyOnceAgainSp;
        protected var _againBtn:GLoadingButton;
        private var _limitNum:BitmapNumberText;
        private var _totalNum:BitmapNumberText;
        protected var _rewardTitle:GImageBitmap;
        protected var _levelNum:BitmapNumberText;
        private var _award:GTileList;
        protected var _boReward:GroupCopyDefenceBossRewardPanel;
        protected var _leftBg:GImageBitmap;
        protected var _leftButtomBg:ScaleBitmap;
        protected var _copyList:GTileList;
        protected var _btnList:GTileList;
        private var _tcopy:TCopy;
        private var _levelArr:Array;
        private var _showArr:Array;
        private var _btnSelIndex:int = 0;
        private var _openLen:int = 3;
        private var _limitTime:int = 1;
        private var _resNum:int = 0;

        public function GroupCopyDefensePanel()
        {
            this._levelArr = [1, 2, 3];
            this._showArr = [];
            return;
        }// end function

        private function onEnter(event:MouseEvent) : void
        {
            var _loc_4:String = null;
            if (this._tcopy == null)
            {
                this._tcopy = CopyConfig.instance.getCopyInfoByCode(DefenseInfo.DefenseCopyCode);
            }
            var _loc_2:* = Cache.instance.task.getTaskByCode(GameConst.GuideDefenceCopyTaskCode, true);
            if (_loc_2 != null && !_loc_2.isComplete() || Cache.instance.task.getTaskByCode(GameConst.GuideDefenceCopyTaskEnterCode, true) != null)
            {
                this._tcopy = CopyConfig.instance.getCopyInfoByCode(GameConst.GuideDefenseCopyCode);
            }
            var _loc_3:* = GroupCopyUtil.getPassTipStr(this._tcopy.code);
            if (_loc_3)
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(70887, this._tcopy.enterMinLevel, _loc_3));
                return;
            }
            if (event.currentTarget == this._againBtn)
            {
                _loc_4 = Language.getStringByParam(70888, GroupCopyOnceAgainSp.NeedNum);
                Alert.isFlowMouse = true;
                Alert.show(_loc_4, null, Alert.OK | Alert.CANCEL, null, this.closeAlert, null, 4);
            }
            else
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupEnterCopyReq, this._tcopy));
            }
            return;
        }// end function

        private function closeAlert(param1:int) : void
        {
            if (param1 == Alert.OK)
            {
                if (!Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, GroupCopyOnceAgainSp.NeedNum))
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(70889));
                }
                else
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupEnterCopyReqOneMore, this._tcopy));
                }
            }
            return;
        }// end function

        private function init(param1:Object = null) : void
        {
            this._totalNum.text = "" + this.getTotalTimes();
            this._limitNum.text = "" + Cache.instance.copy.getGroupCopyEnterNumByGroup(112);
            this.updateAgainBtn();
            return;
        }// end function

        private function getTotalTimes() : int
        {
            var _loc_1:* = CopyConfig.instance.getCopyInfoByCode(112);
            var _loc_2:* = _loc_1.dayNum;
            var _loc_3:* = Cache.instance.copy.getCopyExtraEnterNumByGroup(112);
            return _loc_2 + _loc_3;
        }// end function

        private function showSkin() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            this._leftBg = UIFactory.gImageBitmap(ImagesConst.GroupCopyDefenseBg1, 8, 7, this);
            this._leftButtomBg = UIFactory.bg(7, 414, 584, 138, this, ImagesConst.PanelBg);
            pushUIToDisposeVec(UIFactory.bg(595, 5, 292, 545, this, ImagesConst.PanelBg));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.GroupCopyBg, 605, 413, this));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GroupCopyTxt_1, 600, 457, this));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GroupCopyTxt_2, 768, 455, this));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GroupCopyTxt_3, 821, 458, this));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.GroupCopyTxt_4, 708, 13, this));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.GroupCopySide, 620, 20, this));
            var _loc_1:* = UIFactory.gBitmap(ImagesConst.GroupCopySide, 860, 20, this);
            _loc_1.scaleX = -1;
            pushUIToDisposeVec(_loc_1);
            this._rewardTitle = UIFactory.gImageBitmap(ImagesConst.LevelRwPreview, 436, 327, this);
            this._levelNum = UIFactory.bitmapNumberText(466, 325, "DailyNormalNum.png", 19, 21, -10, this, 9, 1);
            this._levelNum.text = "60";
            this._copyList = UIFactory.tileList(598, 75, 300, 475, this);
            this._copyList.direction = GBoxDirection.VERTICAL;
            this._copyList.verticalGap = 0;
            this._copyList.horizontalGap = -2;
            this._copyList.rowHeight = 95;
            this._copyList.columnWidth = 286;
            this._copyList.columnCount = 1;
            this._copyList.rowCount = 4;
            this._copyList.width = 475;
            this._copyList.setStyle("cellRenderer", DefenseSingleCopyCell);
            this._copyList.configEventListener(Event.CHANGE, this.handleCellRendererChange);
            this._copyList.selectedIndex = 0;
            this.addChild(this._enterBtn);
            this.addChild(this._againSp);
            this.addChild(this._againBtn);
            this.addChild(this._limitNum);
            this.addChild(this._totalNum);
            this.addChild(this._award);
            this.addChild(this._btnList);
            this.openSelect();
            return;
        }// end function

        private function showSkin2() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            this._enterBtn.label = ImagesConst.Copy_Enter;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.groupCopy, this.onResLoad);
            LoaderHelp.addResCallBack(ResFileConst.groupCopyDefense, this.onResLoad);
            return;
        }// end function

        private function onResLoad() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this._resNum + 1;
            _loc_1._resNum = _loc_2;
            if (this._resNum < 2)
            {
                return;
            }
            this._enterBtn = UIFactory.gLabelButton(null, GLabelButton.gLoadingButton, ImagesConst.VipReCharge_upSkin, 661, 500, 156, 39);
            this._enterBtn.name = "DefenseCopy_EnterBtn";
            this._againSp = UIFactory.getUICompoment(GroupCopyOnceAgainSp, 661, 500, this);
            this._againSp.visible = false;
            this._againBtn = UIFactory.gLoadingButton(ImagesConst.AddSignBtn_upSkin, 857, 462, 23, 23, this);
            this._againBtn.label = "";
            this._againBtn.toolTipData = Language.getStringByParam(35226, GroupCopyOnceAgainSp.NeedNum);
            this._limitNum = UIFactory.bitmapNumberText(748, 455, "GroupCopyNum.png", 31, 35, -16, this);
            this._limitNum.text = "11";
            this._totalNum = UIFactory.bitmapNumberText(781, 455, "GroupCopyNum.png", 31, 35, -16, this);
            this._totalNum.text = "10";
            this._award = UIFactory.tileList(401, 351, 175, 70, this);
            this._award.setStyle("cellRenderer", GroupCopyRewardCellRender2);
            this._award.selectable = false;
            this._award.columnWidth = 42;
            this._award.rowHeight = 50;
            this._award.direction = GBoxDirection.HORIZONTAL;
            this._award.dataProvider = new DataProvider(this.getRewardByCode(112));
            this._award.drawNow();
            this._boReward = UIFactory.getUICompoment(GroupCopyDefenceBossRewardPanel, 7, 413, this);
            this._boReward.initReward(DefenseInfo.DefenseCopyCode, 5, 109, 110, 11);
            this._btnList = UIFactory.tileList(600, 36, 294, 50, this);
            this._btnList.direction = GBoxDirection.VERTICAL;
            this._btnList.verticalGap = 2;
            this._btnList.horizontalGap = 2;
            this._btnList.rowHeight = 50;
            this._btnList.columnWidth = 93;
            this._btnList.columnCount = 3;
            this._btnList.rowCount = 1;
            this._btnList.width = 294;
            this._btnList.setStyle("cellRenderer", DefenseCopySingleLvBtnCell);
            this.initTab();
            this._btnList.configEventListener(Event.CHANGE, this.onBtnChange);
            LoaderHelp.addResCallBack(ResFileConst.groupCopy, this.showSkin);
            LoaderHelp.addResCallBack(ResFileConst.copyGroup, this.showSkin2);
            this.addChild(this._boReward);
            this._enterBtn.configEventListener(MouseEvent.CLICK, this.onEnter);
            this._againSp.enterBtn.configEventListener(MouseEvent.CLICK, this.onEnter);
            this._againBtn.configEventListener(MouseEvent.CLICK, this.onEnter);
            this.init();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupCopyGetBoGetRwInfo, DefenseInfo.DefenseCopyCode));
            EventUtil.addNetEvent(ServerCommand.CopyEnterTimesUpdate, this.init, true);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            EventUtil.addNetEvent(ServerCommand.CopyEnterTimesUpdate, this.init, false);
            GroupCopyUtil.btnSelIndex = 0;
            this._resNum = 0;
            LoaderHelp.removeResEvent(ResFileConst.groupCopy, this.onResLoad);
            LoaderHelp.removeResEvent(ResFileConst.groupCopyDefense, this.onResLoad);
            var _loc_2:String = "";
            this._enterBtn.name = "";
            this._leftBg = null;
            this._enterBtn = null;
            this._limitNum = null;
            this._totalNum = null;
            this._award = null;
            this._againBtn = null;
            this._leftButtomBg = null;
            this._againSp = null;
            this._boReward = null;
            this._copyList = null;
            this._rewardTitle = null;
            this._levelNum = null;
            this._btnList = null;
            return;
        }// end function

        private function updateAgainBtn() : void
        {
            var _loc_1:* = CopyConfig.instance.getCopyInfoByCode(112);
            var _loc_2:* = _loc_1.dayNum;
            var _loc_3:* = Cache.instance.copy.getGroupCopyEnterNumByGroup(112);
            this._againSp.updateBtn(_loc_3, _loc_2);
            if (_loc_3 == 1 && this.getTotalTimes() == 1)
            {
                this.setBtn(this._enterBtn, false);
                this._againBtn.enabled = true;
                EffectManager.glowFilterReg(this._againBtn);
            }
            else
            {
                this.setBtn(this._enterBtn, true);
                this._againBtn.enabled = false;
                EffectManager.glowFilterUnReg(this._againBtn);
                this._againBtn.filters = [FilterConst.colorFilter2];
            }
            this.setBtn(this._enterBtn, true);
            return;
        }// end function

        private function setBtn(param1:GLabelButton, param2:Boolean) : void
        {
            if (param2)
            {
                param1.enabled = true;
                param1.filters = null;
            }
            else
            {
                param1.enabled = false;
                param1.filters = [FilterConst.colorFilter2];
            }
            return;
        }// end function

        private function getRewardDataProvider(param1:Array) : DataProvider
        {
            var _loc_3:Object = null;
            var _loc_4:ItemData = null;
            var _loc_6:int = 0;
            var _loc_2:* = new DataProvider();
            var _loc_5:* = param1.length;
            while (_loc_6 < _loc_5)
            {
                
                if (param1[_loc_6])
                {
                    _loc_4 = new ItemData(int(param1[_loc_6]));
                    _loc_3 = {itemData:_loc_4};
                    if (_loc_4.itemInfo && ItemsUtil.isFitCarrer(_loc_4))
                    {
                        _loc_3 = {itemData:_loc_4};
                        _loc_2.addItem(_loc_3);
                    }
                }
                _loc_6++;
            }
            return _loc_2;
        }// end function

        private function getRewardByCode(param1:int) : Array
        {
            var _loc_4:String = null;
            var _loc_5:Array = null;
            var _loc_6:ItemData = null;
            var _loc_7:int = 0;
            var _loc_2:* = CopyConfig.instance.getCopyInfoByCode(param1);
            var _loc_3:Array = [];
            if (_loc_2)
            {
                _loc_4 = _loc_2.rewardShow;
                _loc_5 = _loc_4.split("#");
                for each (_loc_7 in _loc_5)
                {
                    
                    _loc_6 = new ItemData(_loc_7);
                    _loc_3.push(_loc_6);
                }
            }
            return _loc_3;
        }// end function

        private function updateBoReward(param1:int = 0) : void
        {
            param1 = param1 == 0 ? (DefenseInfo.DefenseCopyCode) : (param1);
            this._boReward.initReward(param1, 5);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GroupCopyGetBoGetRwInfo, param1));
            return;
        }// end function

        public function updateBoRwAfterGet() : void
        {
            if (this._boReward)
            {
                this._boReward.updateListAfterGet();
            }
            return;
        }// end function

        private function getCopyDataProvider() : DataProvider
        {
            var _loc_3:Object = null;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = GroupCopyUtil.getDefenseCopyArrByDiff(this._btnList.selectedIndex);
            var _loc_4:* = 0 * 4;
            var _loc_5:* = _loc_2.length;
            var _loc_6:* = _loc_4;
            while (_loc_6 < _loc_5)
            {
                
                _loc_3 = {tCopy:_loc_2[_loc_6]};
                _loc_1.addItem(_loc_3);
                _loc_6++;
            }
            return _loc_1;
        }// end function

        public function updateCopyList() : void
        {
            this._copyList.dataProvider = this.getCopyDataProvider();
            return;
        }// end function

        private function handleCellRendererChange(event:Event = null) : void
        {
            var _loc_2:* = Cache.instance.role.entityInfo.level;
            if ((this._copyList.selectedItem.tCopy as TCopy).enterMinLevel > _loc_2 && _loc_2 <= (this._copyList.selectedItem.tCopy as TCopy).enterMaxLevel)
            {
                return MsgManager.showRollTipsMsg(Language.getStringByParam(70885, (this._copyList.selectedItem.tCopy as TCopy).enterMinLevel));
            }
            this._tcopy = this._copyList.selectedItem.tCopy;
            var _loc_3:* = this._tcopy.rewardShow ? (this._tcopy.rewardShow) : ("");
            var _loc_4:* = this._tcopy.rewardShow.split("#");
            this._award.dataProvider = this.getRewardDataProvider(_loc_4);
            this._award.x = 401 + (170 - 42 * this._award.dataProvider.length) * 0.5;
            this._levelNum.text = "" + this._tcopy.enterMinLevel;
            this.updateBoReward(this._tcopy.code);
            this._leftBg.imgUrl = "GroupCopyDefenseBig_" + this._tcopy.enterMinLevel + ".jpg";
            return;
        }// end function

        private function initTab() : void
        {
            this._showArr = this._levelArr.slice(0, this._openLen);
            var _loc_1:* = new DataProvider(this._showArr);
            this._btnList.dataProvider = _loc_1;
            this._btnList.drawNow();
            return;
        }// end function

        private function openSelect() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_1:* = GroupCopyUtil.checkRewByIndex(0);
            if (_loc_1 && _loc_1.length > 1)
            {
                _loc_2 = _loc_1[1];
                this._btnList.selectedIndex = _loc_2 % this._openLen;
                this.onBtnChange(null);
            }
            else
            {
                _loc_3 = GroupCopyUtil.getHeighestBtnIndex();
                this._btnList.selectedIndex = _loc_3;
                this.onBtnChange(null);
            }
            return;
        }// end function

        private function onBtnChange(event:Event) : void
        {
            GroupCopyUtil.btnSelIndex = this._btnList.selectedIndex;
            this.updateCopyList();
            this.setCopyListSelectIndex();
            this.handleCellRendererChange();
            return;
        }// end function

        private function setCopyListSelectIndex() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Boolean = false;
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            var _loc_1:* = GroupCopyUtil.getRwArrByDiff(this._btnList.selectedIndex);
            if (_loc_1 && _loc_1.length > 1)
            {
                _loc_2 = _loc_1[0];
                this._copyList.selectedIndex = _loc_2;
            }
            else if (Cache.instance.group.isInCopyGroup)
            {
                _loc_3 = this._copyList.dataProvider.length;
                _loc_4 = false;
                while (_loc_5 < _loc_3)
                {
                    
                    _loc_6 = this._copyList.dataProvider.getItemAt(_loc_5);
                    if (_loc_6.tCopy.code == Cache.instance.group.sgroup.copyCode)
                    {
                        this._copyList.selectedIndex = _loc_5;
                        _loc_4 = true;
                        break;
                    }
                    _loc_5++;
                }
            }
            else
            {
                this._copyList.selectedIndex = GroupCopyUtil.getHighestLvIndex(this._btnList.selectedIndex);
            }
            return;
        }// end function

    }
}
